<?php

namespace app\common\model;

use think\Model;
use think\Db;

/**
 * 代理申请表
 */
class Proxylog Extends Model
{

    // 表名
    protected $name = 'proxylog';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 加入代理检查是否已经有或者申请中或者审核通过
     *
     * @param $number
     * @param $province
     * @param $city
     * @param $area
     * @param $regione
     * @param $user_id
     * @return mixed
     */
   public function proxyIsCheck($number,$province,$city,$area,$regione,$user_id){
       $data['code']=0;
       $data['msg']='';
       try{

           switch ($number){
               case 1:
                   $where['province']=array('eq',$province);
                   $where['server_type']=array('eq',1);
                   $msg='省代理已有存在';
                   break;
               case 2:
                   $where['city']=array('eq',$city);
                   $where['server_type']=array('eq',2);
                   $msg='市代理已有存在';
                   break;
               case 3:
                   $where['area']=array('eq',$area);
                   $where['server_type']=array('eq',3);
                   $msg='区代理已有存在';
                   break;
               case 4:
                   $where['is_agent']=array('eq',1);
                   $where['user_id']=array('eq',$user_id);
                   $where['status']=array('in',array(0,1));
                   $msg='已经提交申请，正在审核中';
                   break;
               case 5:
                   $where['regione']=array('eq',$regione);
                   $where['server_type']=array('eq',5);
                   $msg='大区区代理已有存在';
                   break;
               default:
                   return $data;
                   break;
           }
           if($number==4){
               $Personal_agency=Db::name('proxylog')
                   ->where($where)
                   ->count();
               if($Personal_agency>0){
                   $data['code']=1;
                   $data['msg']=$msg;
               }
           }else{
               $districtCount=Db::name('user')
                   ->where($where)
                   ->count();
               if($districtCount>0){
                   $data['code']=1;
                   $data['msg']=$msg;
               }else{
                   $where['status']=array('eq',1);
                   $count=Db::name('proxylog')
                       ->where($where)
                       ->count();
                   if($count>0){
                       $data['code']=1;
                       $data['msg']=$msg;
                   }else{
                       $districtWhere['server_type']=array('in',array(1,2,3,5));
                       $districtWhere['user_id']=array('eq',$user_id);
                       $districtWhere['status']=array('in',array(0,1));
                       $district_agent=Db::name('proxylog')->where($where)->count();
                       if($district_agent>0){
                           $data['code']=1;
                           $data['msg']='已经提交申请，正在审核中';
                       }
                   }
               }

           }

       }catch (\Exception $e){
            return $data;
       }
       return $data;
   }

}
